import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';
import type { Key } from 'ant-design-vue/es/vc-tree/interface';

import type { Ref } from 'vue';

import type { Undefinedable } from '@vben/types';

import type { FormRulesExt } from '#/api';
import type { IotDeviceModel } from '#/api/iot/device/model/iotDeviceModel';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { dict } from '@fast-crud/fast-crud';

import { IotCustomersApi } from '#/api/iot/customers/iotCustomers';
import { IotDeviceApi } from '#/api/iot/device/iotDevice';
import { ActionEnum, DictEnum } from '#/enums/commonEnum';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
  STATE_CONSTANT_DICT,
  transformQuery,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();

type ContextRef = {
  tenantIds: Ref<Undefinedable<Key[]>>;
};

export function createCrudOptions(
  props: CreateCrudOptionsProps<IotDeviceModel.IotDeviceResultVO, ContextRef>,
): CreateCrudOptionsRet<IotDeviceModel.IotDeviceResultVO> {
  const selectedRowKeys = ref<string[]>([]);
  return {
    crudOptions: {
      request: {
        pageRequest: IotDeviceApi.pageRequest,
        addRequest: IotDeviceApi.addRequest,
        editRequest: IotDeviceApi.editRequest,
        delRequest: IotDeviceApi.delRequest,
        infoRequest: IotDeviceApi.infoRequest,
        transformQuery: (query) => {
          const tq = transformQuery(query);
          tq.model.tenantIdList = props.context.tenantIds.value;
          return tq;
        },
      },
      actionbar: {
        buttons: {
          add: {
            show: hasPermission('iot:device:add'),
          },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: IotDeviceApi.remove,
            role: 'iot:device:del',
          }),
        },
      },
      form: {
        wrapper: {
          is: 'a-modal',
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: {
            show: hasPermission('iot:device:edit'),
          },
          view: {
            show: true,
          },
          remove: {
            show: hasPermission('iot:device:del'),
          },
          copy: {
            show: hasPermission('iot:device:copy'),
          },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        code: {
          title: $t('iot.device.iotDevice.code'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          column: {
            show: true,
          },
        },
        iccid: {
          title: $t('iot.device.iotDevice.iccid'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
            required: true,
          },
          editForm: {
            required: true,
            show: true,
          },
          column: {
            show: true,
          },
        },
        name: {
          title: $t('iot.device.iotDevice.name'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          column: {
            show: true,
          },
        },
        type: {
          title: $t('iot.device.iotDevice.type'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.DeviceTypeEnum),
          search: { show: true },
          addForm: {
            show: true,
            value: '1',
          },
          editForm: {
            component: {
              disabled: false,
            },
            required: true,
            show: true,
          },
          column: {
            show: true,
          },
        },
        tenantId: {
          title: '位置',
          type: 'dict-tree',
          addForm: {
            show: false,
          },
          editForm: {
            show: false,
          },
          form: {
            component: { multiple: true },
          },
          column: { component: { type: 'text' } },
          dict: dict({
            isTree: true,
            value: 'id',
            label: 'name',
            async getData() {
              return await IotCustomersApi.treeToDevice({});
            },
          }),
        },
        resource: {
          title: $t('iot.device.iotDevice.resource'),
          type: 'dict-radio',
          dict: backendDict({
            type: DictEnum.DeviceResourceType,
          }),
          search: { show: true },
          addForm: {
            show: true,
            value: '0',
          },
          column: {
            show: true,
          },
        },
        expTime: {
          title: $t('iot.device.iotDevice.expTime'),
          type: 'datetime',
          search: { show: false },
          form: {
            value: $t('common.permanent'),
          },
          addForm: {
            show: false,
          },
          editForm: {
            show: false,
          },
          column: {
            show: true,
            formatter: ({ value }) => {
              // 当值为空时显示"永久"
              return value || $t('common.permanent');
            },
          },
        },
        status: {
          title: $t('iot.device.iotDevice.status'),
          type: 'dict-switch',
          dict: STATE_CONSTANT_DICT,
          search: { show: true },
          addForm: {
            show: true,
            value: false,
          },
          column: {
            show: true,
          },
        },
        letStatus: {
          title: $t('iot.device.iotDevice.letStatus'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.LetStatusEnum),
          search: { show: true },
          addForm: {
            show: true,
            value: '0',
            required: true,
          },
          column: {
            show: true,
          },
        },
        letExpTime: {
          title: $t('iot.device.iotDevice.letExpTime'),
          type: 'datetime',
          search: { show: true },
          form: {
            value: $t('common.permanent'),
          },
          addForm: {
            show: false,
          },
          editForm: {
            show: false,
          },
          column: {
            show: true,
            formatter: ({ value }) => {
              // 当值为空时显示"永久"
              return value || $t('common.permanent');
            },
          },
        },

        protocolType: {
          title: $t('iot.device.iotDevice.protocolType'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.DeviceComunicationType),
          search: { show: true },
          addForm: {
            show: true,
            value: 'TCP',
          },
          column: {
            show: true,
          },
        },
        hardVersion: {
          title: $t('iot.device.iotDevice.hardVersion'),
          type: 'text',
          search: { show: true },
          form: {
            value: '1.0.0',
          },
          addForm: {
            show: true,
          },
          column: {
            show: true,
          },
        },
        ...createdTimeColumn({}),
        remark: {
          title: $t('iot.device.iotDevice.remark'),
          type: 'textarea',
          search: { show: false },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
